clear all;
clc;
load mnist_test;

l = 50;
s = 30;
C = 10;

X = xU(1:100)';
Y = yU';
Y = Y(1:l,:);
Y = [(Y==0) (Y==1) (Y==2) (Y==3) (Y==4) (Y==5) (Y==6) (Y==7) (Y==8) (Y==9)];
Y = Y - (Y==0);
Y = double(Y);
n = size(X,1);
[l,c] = size(Y);

K = X*X';
clear X;
K = K/max(max(K));
[V,D] = eigs(K,s);
D = inv(D);

P = zeros(l,s,c);
for i = 1:c
    P(:,:,i) = repmat(Y(:,i),1,s).*V(1:l,:);
end

Q = V(l+1:n,:);
clear V;
tic
cvx_begin sdp
cvx_solver sedumi
    variable t(c,1)
    variable gam(s,c)
    variable bb(c,1)
    variable del(l,1) nonnegative
    variable bet(l,1) nonnegative
    minimize (ones(1,c)*gam'*D*gam*ones(c,1)+2*C*ones(1,l)*del);
        for i = 1:c
            P(:,:,i)*gam(:,i)+ bb(i)*Y(:,i)-ones(l,1)+del-bet == 0;
        end
cvx_end
toc

[~,Ypred] = max(Q*gam+repmat(bb',n-l,1),[],2);
Ypred = uint8(Ypred)-1;

label = int8(yU(l+1:size(yU,2))');
acc = calacc(Ypred,label);
save(['res/LINL',int2str(l),'S',int2str(s)]);